package cz.possoft.client.utils;

import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import javax.swing.JApplet;

import cz.possoft.client.gui.panel.GamePanel;
import cz.possoft.client.gui.panel.nastavenispodnipanel.panel.NastaveniShowPanel;

/**
  * Trida plni staticky ucel na dotaz pro aktualni instance jednotlivych panelu
  * 
  * @author mara
  */
public class PanelHolderUtils {

	// aktualni instance zobrazeneho herniho panelu
	private static GamePanel currentGamePanel;
	
	// aktualni instance pusteneho appletu (aplikace)
	private static JApplet applet;
	
	// pole vsech hracich panelu
	private static Map<String, GamePanel> gamePanels = new ConcurrentHashMap<String, GamePanel>();
	
	/** Called from LoaderHolder when new game is deployed. */
	public static void handleDeployment(String gameKey) {
		GamePanel gamePanel = GameInvocationUtils.getInstanceOfGamePanel(gameKey);
		gamePanel.setGameClassPrefix(gameKey);
		gamePanel.init();			
		gamePanels.put(gameKey, gamePanel);
		gamePanel.setVisible(false);
		applet.getContentPane().add(gamePanel, "0,0,0,1");
		NastaveniShowPanel.getGamesComboBox().addItem(gamePanel.getDisplayName());
		// this is the first panel. Set it as current.
		if (gamePanels.size() == 1) {
			setCurrentGamePanel(gamePanel);
		}
	}
	
	/** Called from LoaderHolder when game is undeployed */
	public static void handleUndeployment(String gameKey) {
		GamePanel gPanelToRemove = gamePanels.get(gameKey);
		gPanelToRemove.destroy();
		gamePanels.remove(gameKey);
		if ((currentGamePanel != null) && (currentGamePanel.equals(gPanelToRemove))) {
			if (gamePanels.isEmpty()) {
				setCurrentGamePanel(null);
			}
			else {
				String nextKey = gamePanels.keySet().iterator().next();
				setCurrentGamePanel(gamePanels.get(nextKey));
			}
		}
		applet.getContentPane().remove(gPanelToRemove);
		NastaveniShowPanel.getGamesComboBox().removeItem(gPanelToRemove.getDisplayName());
	}
	
	/** vraci vsechny gamePanely v kolekci. */
	protected static Collection<GamePanel> getAllGamePanels() {
		return Collections.unmodifiableCollection(gamePanels.values());
	}
	
	/** List of game keys for all cuurently loaded games. For example: {"Chess", "Dama", "Four"} */
	public static Collection<String> getKeysForAllGames() {
		Collection<GamePanel> panels = getAllGamePanels();
		Collection<String> keys = new HashSet<String>();
		for (GamePanel panel : panels) {
			keys.add(panel.getGameClassPrefix());
		}
		return Collections.unmodifiableCollection(keys);		
	}
	
	/** Retrun game panel by it's key */
	public static GamePanel getGamePanelByKey(String key) {
		for (GamePanel panel : getAllGamePanels()) {
			if (panel.getGameClassPrefix().equals(key)) {
				return panel;
			}
		}
		return null;
	}
	
	/** Return display name of panel by it's key */ 
	public static String getDisplayNameByKey(String key) {
		for (GamePanel panel : getAllGamePanels()) {
			if (panel.getGameClassPrefix().equals(key)) {
				return panel.getDisplayName();
			}
		}
		return null;		
	}
	
	/** List of games in human-readable format. For example: {"Sachy", "Dama", "Piskvorky"} */
	public static Collection<String> getDisplayNamesForAllGames() {
		Collection<GamePanel> panels = getAllGamePanels();
		Collection<String> keys = new HashSet<String>();
		for (GamePanel panel : panels) {
			keys.add(panel.getDisplayName());
		}
		return Collections.unmodifiableCollection(keys);
	}
	
	// jako currentGamePanel nastavi panel z listu gamePanels podle predlozeneho keye
	// Navic vraci tento gamePanel
	public static GamePanel setCurrentGamePanelFromPrefix(String gameKey) {
		        GamePanel gamePanel = gamePanels.get(gameKey); 
				setCurrentGamePanel(gamePanel);											
		        return gamePanel;
	}
	
	// nastavuje aktualni instanci zobrazeneho herniho panelu
	public static void setCurrentGamePanel(GamePanel currGP) {
		if (currentGamePanel != null) {			
			currentGamePanel.setVisible(false);			
		}
		currentGamePanel = currGP;
		if (currentGamePanel != null) {
			currentGamePanel.setVisible(true);
		}
	}
	
	// aktualni instance zobrazeneho herniho panelu
	public static GamePanel getCurrentGamePanel() {
		return currentGamePanel;
	}	

	public static JApplet getApplet() {
		return applet;
	}

	public static void setApplet(JApplet applet) {
		PanelHolderUtils.applet = applet;
	}		
	
}
